
const { MongoClient } = require('mongodb');
const { DATABASE_URL, DATABASE_NAME } = require('../config/database')


const funcConfig = require('./config')

async function main(tableName, filter, type) {
    const client = new MongoClient(DATABASE_URL);
    await client.connect();
    const db = client.db(DATABASE_NAME);
    const collection = db.collection(tableName);
    const result = await funcConfig[type](collection, filter);
    client.close();
    return result;
}

// 数据库操作
function actionDb(tableName, filter, type) {
    return new Promise((resolve, reject) => {
        main(tableName, filter, type)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
        // .finally(() => client.close());
    })

}

module.exports = actionDb;